6 Code smells

 

In this chapter

  • Handling Constructor Over-injection code smells
  • Detecting and preventing overuse of Abstract Factories
  • Fixing cyclic Dependency code smells

You may have noticed that I (Mark) have a fascination with sauce béarnaise—or sauce hollandaise. One reason is that it tastes so good; another is that it’s a bit tricky to make. In addition to the challenges of production, it presents an entirely different problem: it must be served immediately (or so I thought).

This used to be less than ideal when guests arrived. Instead of being able to casually greet my guests and make them feel welcome and relaxed, I was frantically whipping the sauce in the kitchen, leaving them to entertain themselves. After a couple of repeat performances, my sociable wife decided to take matters into her own hands. We live across the street from a restaurant, so one day she chatted with the cooks to find out whether there’s a trick that would enable me to prepare a genuine hollandaise well in advance. It turns out there is. Now I can serve a delicious sauce for my guests without first subjecting them to an atmosphere of stress and frenzy.

6.1 Dealing with the Constructor Over-injection code smell

6.1.1 Recognizing Constructor Over-injection

6.1.2 Refactoring from Constructor Over-injection to Facade Services

6.1.3 Refactoring from Constructor Over-injection to domain events

6.2 Abuse of Abstract Factories

6.2.1 Abusing Abstract Factories to overcome lifetime problems

6.2.2 Abusing Abstract Factories to select Dependencies based on runtime data

6.3 Fixing cyclic Dependencies

6.3.1 Example: Dependency cycle caused by an SRP violation

6.3.2 Analysis of Mary’s Dependency cycle

6.3.3 Refactoring from SRP violations to resolve the Dependency cycle

6.3.4 Common strategies for breaking Dependency cycles

6.3.5 Last resort: Breaking the cycle with Property Injection

Summary

sitemap